﻿using System;
using System.Globalization;
using curbside.Core;
using System.Text.RegularExpressions;
using System.Xml;

namespace curbside.Extensions
{
    /// <summary>
    /// String 扩展方法
    /// </summary>
    public static class StringExtensions
    {
        #region Old methods

        /// <summary>
        /// 判断字符串是否为空
        /// </summary>
        /// <param name="str">任意字符串</param>
        /// <returns>如果为空，返回true；否则，返回false</returns>
        public static bool IsNullOrEmpty(this String str)
        {
            return string.IsNullOrWhiteSpace(str);
        }

        /// <summary>
        /// 从字符串中移除指定前缀
        /// </summary>
        /// <param name="sourceString">任意字符串</param>
        /// <param name="prefix">指定的前缀</param>
        /// <param name="ignoreCase">是否忽略大小写</param>
        /// <returns>移除前缀后的字符串</returns>
        public static string TrimPrefix(this string sourceString, string prefix, bool ignoreCase)
        {
            prefix = prefix ?? string.Empty;

            if (!sourceString.StartsWith(prefix, ignoreCase, CultureInfo.CurrentCulture))
            {
                return sourceString;
            }

            return sourceString.Remove(0, prefix.Length);
        }

        /// <summary>
        /// 从字符串中移除指定后缀
        /// </summary>
        /// <param name="sourceString">任意字符串</param>
        /// <param name="prefix">指定的后缀</param>
        /// <param name="ignoreCase">是否忽略大小写</param>
        /// <returns>移除后缀后的字符串</returns>
        public static string TrimSuffix(this string sourceString, string suffix, bool ignoreCase)
        {
            suffix = suffix ?? string.Empty;

            if (!sourceString.EndsWith(suffix, ignoreCase, CultureInfo.CurrentCulture))
            {
                return sourceString;
            }

            return sourceString.Substring(0, sourceString.Length - suffix.Length);
        }

        /// <summary>
        /// 转全角(SBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>全角字符串</returns>
        public static string ToSBC(this string input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 32)
                {
                    c[i] = (char)12288;
                    continue;
                }
                if (c[i] < 127)
                    c[i] = (char)(c[i] + 65248);
            }

            return new string(c);
        }

        /// <summary>
        /// 转半角(DBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>半角字符串</returns>
        public static string ToDBC(this string input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }

            return new string(c);
        }

        

        #endregion

        public static ConvertString AsConvertable(this string s)
        {
            return new ConvertString(s);
        }

        public static CheckString AsCheckable(this string s)
        {
            return new CheckString(s);
        }
    }

    public class BaseString
    {
        protected string s;
        public BaseString(string s) { this.s = s; }
        internal string Value
        {
            get { return s; }
        }
    }
}
